﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Parameters;

namespace QLNSClient
{
    public partial class uc_ThongkeKL : UserControl
    {

        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public uc_ThongkeKL()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            grc_Chitiet.Visible = false;

            t = DateTime.Today;

            int namdau = 2009;
            int namcuoi = 2014;
            int t1 = Int32.Parse(t.Year.ToString());
            if ((t1 - namcuoi) > 0)
            {
                namdau = namdau + 1;
                namcuoi = namcuoi + 1;
            }

            int tempT = DateTime.Today.Year;
            for (int i = namdau; i <= namcuoi; i++)
            {
                cmb_Nam.Items.Add(i);
            }
            cmb_Nam.Text = tempT.ToString();


            gv_ThongkeKL.DataSource = sv.getThongkeKLtheoNam(t);
            rdg_thongkeKL.SelectedIndex = 0;

            lke_DMDV.Properties.DataSource = sv.getDMDV();
            lke_DMDV.Enabled = false;

        }

        private void uc_ThongkeKL_Load(object sender, EventArgs e)
        {
            LoadData();
        }


        int index;
        private void rdg_thongkeKL_SelectedIndexChanged(object sender, EventArgs e)
        {
            index = Int32.Parse(rdg_thongkeKL.EditValue.ToString());
            if (rdg_thongkeKL.SelectedIndex != 0 && lke_DMDV.EditValue.ToString()=="")
            {
                gv_ThongkeKL.DataSource = sv.getThongKeKL(index, t);
                lbl_TS.Text = sv.countSLKL(index,t).ToString();
            }
            if(rdg_thongkeKL.SelectedIndex==0 && lke_DMDV.EditValue.ToString()=="")
            {
                gv_ThongkeKL.DataSource = sv.getThongkeKLtheoNam(t);
                lbl_TS.Text = sv.countKLtheoNam(t).ToString();
            }
            if (rdg_thongkeKL.SelectedIndex != 0 && lke_DMDV.EditValue.ToString() != "")
            {
                gv_ThongkeKL.DataSource = sv.getTKKLtheoNamDVMaKL(index, a, t);
                lbl_TS.Text = sv.countKLtheoNamDVMaKT(index, a, t).ToString();
            }
            if (rdg_thongkeKL.SelectedIndex == 0 && lke_DMDV.EditValue.ToString() != "")
            {
                gv_ThongkeKL.DataSource = sv.getTTKLtheoNamDV(a, t);
                lbl_TS.Text = sv.countKLtheoNamDV(a, t).ToString();
            }
        }


        DateTime t;
        private void cmb_Nam_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a = cmb_Nam.Text;
            t = new DateTime();
            t = DateTime.ParseExact(a, "yyyy", null);
           
            gv_ThongkeKL.DataSource = sv.getThongkeKLtheoNam(t);
            rdg_thongkeKL.SelectedIndex = 0;

            grc_Chitiet.Visible = true;

            lbl_khientrach.Text = sv.countKhientrach(t).ToString();
            lbl_canhcao.Text = sv.countCanhcao(t).ToString();
            lbl_habacluong.Text = sv.countHabacluong(t).ToString();
            lbl_giangchuc.Text = sv.countGiangchuc(t).ToString();
            lbl_cachchuc.Text = sv.countCachchuc(t).ToString();
            lbl_buocthoiviec.Text = sv.countBuocthoiviec(t).ToString();

            if (sv.countKLtheoNam(t) != 0)
            {
                lbl_TS.Text = sv.countKLtheoNam(t).ToString();
            }
            else
            {
                MessageBox.Show("Không có dữ liệu!");            
            }

        }

        private void btn_rptKL_Click(object sender, EventArgs e)
        {
            
            
            
                if (rdg_thongkeKL.SelectedIndex == 0)
                {
                    klkl rptKL = new klkl();
                    using (ReportPrintTool printTool = new ReportPrintTool(rptKL))
                    {
                        Parameter Nam = new Parameter();
                        Nam.Name = "Choosed";
                        Nam.Type=typeof(System.Int32);
                        Nam.Value = t.Year;
                        rptKL.Parameters.Add(Nam);
                        Nam.Visible = false;
                        rptKL.FilterString="[Nam]=?Choosed";

                        rptKL.ShowRibbonPreviewDialog();
                        
                    }
                }
                else
                {
                    rpt_All_KL rptKL = new rpt_All_KL();
                    using (ReportPrintTool printTool = new ReportPrintTool(rptKL))
                    {
                        rptKL.DataSource = sv.getReportLoaiKL(index, t);
                    }
                    rptKL.ShowRibbonPreviewDialog();
                }
        }


        string a;
        private void lke_DMDV_EditValueChanged(object sender, EventArgs e)
        {
            rdg_thongkeKL.SelectedIndex = 0;
            a = lke_DMDV.EditValue.ToString();
            gv_ThongkeKL.DataSource = sv.getTTKLtheoNamDV(a, t);
            lbl_TS.Text = sv.countKLtheoNamDV(a, t).ToString();
            lbl_khientrach.Text = sv.countKhientrachtheoNamDV(a, t).ToString();
            lbl_canhcao.Text = sv.countCanhcaotheoNamDV(a,t).ToString();
            lbl_habacluong.Text = sv.countHabacluongtheoNamDV(a,t).ToString();
            lbl_giangchuc.Text = sv.countGiangchuctheoNamDV(a, t).ToString();
            lbl_cachchuc.Text = sv.countCachchuctheoNamDV(a, t).ToString();
            lbl_buocthoiviec.Text = sv.countBuocthoiviectheoNamDV(a,t).ToString();
        }

        private void ck_DMDV_CheckedChanged(object sender, EventArgs e)
        {
            if (ck_DMDV.Checked == true)
            {
                rdg_thongkeKL.SelectedIndex = 0;
                gv_ThongkeKL.DataSource = sv.getThongkeKLtheoNam(t);
                lke_DMDV.Enabled = true;
                lke_DMDV.Properties.DataSource = sv.getDMDV();
                lbl_TS.Text=sv.countKLtheoNam(t).ToString();
                lbl_khientrach.Text = sv.countKhientrach(t).ToString();
                lbl_canhcao.Text = sv.countCanhcao(t).ToString();
                lbl_habacluong.Text = sv.countHabacluong(t).ToString();
                lbl_giangchuc.Text = sv.countGiangchuc(t).ToString();
                lbl_cachchuc.Text = sv.countCachchuc(t).ToString();
                lbl_buocthoiviec.Text = sv.countBuocthoiviec(t).ToString();
            }
            else
            {
                lke_DMDV.Enabled = false;
                lke_DMDV.EditValue = "";
                gv_ThongkeKL.DataSource = sv.getThongkeKLtheoNam(t);
                rdg_thongkeKL.SelectedIndex = 0;
                lbl_TS.Text=sv.countKLtheoNam(t).ToString();
                lbl_khientrach.Text = sv.countKhientrach(t).ToString();
                lbl_canhcao.Text = sv.countCanhcao(t).ToString();
                lbl_habacluong.Text = sv.countHabacluong(t).ToString();
                lbl_giangchuc.Text = sv.countGiangchuc(t).ToString();
                lbl_cachchuc.Text = sv.countCachchuc(t).ToString();
                lbl_buocthoiviec.Text = sv.countBuocthoiviec(t).ToString();
            }
        }
    }
}
